package flex.cc.drive.util;


import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.util.encoders.UrlBase64;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DriveDESUtil {
	
    private static final String CRYPT_ALGORITHM = "DESede";

    //key3Des
    //private static String key3Des= "C205BC5839533270jUN1d77Y";

    /**
     * 解密
     * @param value
     * @param sourceKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String value,String sourceKey,String key3Des) {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key3Des.getBytes("UTF-8"), CRYPT_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CRYPT_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            byte[] decodedByte = UrlBase64.decode(value.getBytes("UTF-8"));
            byte[] decryptedByte = cipher.doFinal(decodedByte);
            String jsonStr = new String(decryptedByte,"UTF-8");
            String decryptStr = jsonStr.split("[|]")[0];
            String sign = jsonStr.split("[|]")[1];
            boolean sigResult = verifyTheEncryKey(decryptStr,sourceKey,sign);
            if(sigResult){
                return decryptStr;
            }else {
                log.error("PCThreeDESUtil.decrypt,验签失败！");
            }
        } catch(Exception e) {
            log.error("PCThreeDESUtil.decrypt : decrypt was failed!", e);
        }
        return "";
    }

    /**
     * 加密
     * @param value
     * @return
     * @throws Exception
     */
    public static String encrypt(String value,String key3Des) throws Exception{
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key3Des.getBytes("UTF-8"), CRYPT_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CRYPT_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);

            byte[] encryptedByte = cipher.doFinal(value.getBytes("UTF-8"));
            byte[] encodedByte = UrlBase64.encode(encryptedByte);
            return new String(encodedByte,"UTF-8");
        } catch(Exception e) {
            log.error("PCThreeDESUtil.encrypt : encrypt was failed!", e);
            throw e;
        }
    }

    //验签
    public static boolean verifyTheEncryKey(String decryptStr, String encryKey, String sign) {
        StringBuffer stn = new StringBuffer();
        stn.append(decryptStr).append(encryKey);
        String paramSign = null;
        try {
            paramSign = DigestUtils.md5Hex(stn.toString().getBytes("UTF-8")).toUpperCase();
        } catch (UnsupportedEncodingException e1) {
        }
        if(!sign.equals(paramSign)){
            //验签失败
            return false;
        }else{
            //验签成功
            return true;
        }
    }
    
    //代驾预约加密
    public static String driverEncrypt(String value,String key,String key3Des) throws Exception{
    	String beforeT = value+key;
        String md5Str = DigestUtils.md5Hex(beforeT.getBytes("UTF-8")).toUpperCase();
        return encrypt(value+"|"+md5Str,key3Des);
    }
    
    //代驾预约解密
    public static String driverDecrypt(String value,String key,String key3Des) throws Exception{
      String jsonStr = decrypt(value, key,key3Des);
      System.out.println("解密数据："+jsonStr);
      return jsonStr;
    }
}
